// pages/idcard/idcard.js
var Bmob = require('../../dist/Bmob-1.6.0.min.js');
const {
  $Message
} = require('../../dist/base/index');
var app = getApp()
Page({
  data: {
    name: null,
    avatarUrl: null,
    gender: null,
    userInfo: null,
    ignore: false,
    userNowId: '',
    userId: 'null',
    area: ['宝山校区', '嘉定校区', '延长校区', '不知道在哪'],
    areaIndex: 0,
    multiArray: [['每天', '工作日', '周末'], ['上午', '下午', '晚上', '全天']],//二维数组，长度是多少是几列  
    multiIndex: [0, 0],
    isOk:false

  },
  onLoad: function (user) {
    let that = this;
    that.setData({
      userInfo: app.globalData.userInfo,
      userNowId: user.objectId,
      userId: app.globalData.authData.objectId
    });

    console.log('访问 user', user.objectId, '的个人信息')
    const pointer = Bmob.Pointer('_User')
    const poiID = pointer.set(that.data.userNowId)

    const query = Bmob.Query('UserData')
    query.include('major', 'post')
    //userId 字段名称关联用户表 ，类型Pointer
    query.equalTo("user", "==", poiID);
    query.find().then(res => {
      //获取到用户add信息之后先获取用户的微信昵称和头像做默认值，如果add信息里面有相同内容覆盖
      const query1 = Bmob.Query('_User')
      query1.get(that.data.userNowId).then(res1 => {
        console.log("res1", res1)
        console.log("res", res)
        //设置默认值
        that.setData({
          name: res1.nickName,
          avatarUrl: res1.userPic,
          gender: 2,
        })
        if (res.length) {
          console.log("查询到用户的UserData 显示服务器的信息")
          console.log('app.globalData.userInfo.gender:', app.globalData.userInfo.gender)
          //首先要把gender设置成2 也是就未知
          var gender = 2;
          //然后看看数据库有没有相对应的数据 如果没有就不设置gender的值
          if (res[0].gender == undefined){
            console.log('gender undefined')
          }
          else if (res[0].gender == false) {
            console.log('gender false')
            gender = 0
          }
          else if (res[0].gender == true)
          {
            console.log('gender true')
            gender = 1
          }


          that.setData({
            areaIndex: parseInt(res[0].area),
            time: res[0].time,
            major: res[0].major.name,
            name: res[0].nickName,
            avatarUrl: res[0].userPic.url,
            gender: gender,
          })
        }
        else {
          console.log("未查询到用户的UserData 显示用户的默认信息")

          that.setData({
            areaIndex: 3,
            time: [0, 3],
            major: '假装是个专业学院',
          })
        }
        that.setData({
          isOk:true
        })

      }).catch(err => {
        console.log(err)

        that.setData({
          isOk: true
        })
      })
    })

  },
  getIn: function () {
    $Message({
      content: '假装加入成功。',
      type: 'success'
    });
  },
  talkTo: function () {
    $Message({
      content: '假装聊天。',
      type: 'success'
    });
  },
  changeInfo: function () {
    wx.navigateTo({
      url: '../../pages/idchange/idchange',
    })
  },
  onChange(event) {
    const detail = event.detail;
    this.setData({
      'switch1': detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})